package com.kfm.base.oop3;

public class CloneDemo {

    public static void main(String[] args) throws CloneNotSupportedException {
        /*
         Object clone 方法复制对象, 浅克隆
         */
        Dog dog = new Dog("泰迪", "棕色");

//        Dog dog1 = new Dog(dog.getName(), dog.getColor());

        Object dog1 = dog.clone();

        System.out.println(dog);
        System.out.println(dog1);

        System.out.println(dog.hashCode());
        System.out.println(System.identityHashCode(dog));
        System.out.println(dog1.hashCode());
        System.out.println(System.identityHashCode(dog1));
    }
}

class Dog implements Cloneable {

    private String name;

    private String color;

    public Dog(String name, String color){
        this.name = name;
        this.color = color;
    }

    @Override
    public String toString() {
        return "有一只" + color + "的狗, 名字叫" + name;
    }

    public String getName(){
        return name;
    }

    public String getColor(){
        return color;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
